package com.example.stock.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component
@Aspect
public class ParamOutAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParamOutAspect.class);

    @Pointcut("execution(* com.example.stock.controller.*Controller.*(..))")
    public void BrokerAspect(){

    }

    @Around("BrokerAspect()")
    public Object processLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        //获取方法名称
        String methodName = method.getName();
        //获取参数
        Object[] args = joinPoint.getArgs();
        JSONObject rqsJson = new JSONObject();
        rqsJson.put("rqsMethod", methodName);
        LOGGER.info(methodName + "-------- config 统一日志 请求信息为：{}: [{}]" ,methodName, JSON.toJSONString(args));
        Object resObj;
        long startTime = System.currentTimeMillis();
        try {
            //执行原方法
            resObj = joinPoint.proceed();
        } catch (Throwable e) {
            LOGGER.error(methodName + "方法执行异常! 耗时 : {} ms", System.currentTimeMillis() - startTime);
            throw e;
        }
        LOGGER.info(methodName + " 耗时 : {} ms", System.currentTimeMillis() - startTime);


        // 打印耗时的信息
        return resObj;
    }

}